/**
* 验证角色属性是否合法，并返回应有值
* @param {Object} character - 角色数据
* @param {Object} job - 职业模板数据
* @returns {{ isLegal: boolean, data: Object }}
*/
async function _validateCharacterData(character, job) {
    const level = character.level;

    // 安全读取基础值（防止 null/undefined）
    const base_hp = parseFloat(job.base_hp) || 0;
    const base_mp = parseFloat(job.base_mp) || 0;
    const base_attack = parseFloat(job.base_attack) || 0;
    const base_defense = parseFloat(job.base_defense) || 0;
    const base_magic_attack = parseFloat(job.base_magic_attack) || 0;
    const base_magic_defense = parseFloat(job.base_magic_defense) || 0;
    const base_speed = parseFloat(job.base_speed) || 0;
    const base_critical_rate = Math.max(0, parseFloat(job.base_critical_rate) || 0);

    const growth_hp = parseFloat(job.growth_hp) || 0;
    const growth_attack = parseFloat(job.growth_attack) || 0;
    const growth_defense = parseFloat(job.growth_defense) || 0;
    const growth_speed = parseFloat(job.growth_speed) || 0;

    // 计算理论值
    const expected = {
        base_hp: parseFloat((base_hp + (level - 1) * growth_hp).toFixed(2)),
        base_mp: parseFloat((base_mp + (level - 1) * growth_hp).toFixed(2)), 
        attack: parseFloat((base_attack + (level - 1) * growth_attack).toFixed(2)),
        defense: parseFloat((base_defense + (level - 1) * growth_defense).toFixed(2)),
        magic_attack: parseFloat((base_magic_attack + (level - 1) * growth_attack).toFixed(2)),
        magic_defense: parseFloat((base_magic_defense + (level - 1) * growth_defense).toFixed(2)),
        speed: parseFloat((base_speed + (level - 1) * growth_speed).toFixed(2)),
        critical_rate: parseFloat(base_critical_rate.toFixed(4)) // 暴击率一般不变
    };

    // 判断是否匹配（注意浮点数比较）
    const tolerance = 0.001; // 允许微小误差
    const isLegal =
        Math.abs(expected.base_hp - character.base_hp) < tolerance &&
        Math.abs(expected.base_mp - character.base_mp) < tolerance &&
        Math.abs(expected.attack - character.attack) < tolerance &&
        Math.abs(expected.defense - character.defense) < tolerance &&
        Math.abs(expected.magic_attack - character.magic_attack) < tolerance &&
        Math.abs(expected.magic_defense - character.magic_defense) < tolerance &&
        Math.abs(expected.speed - character.speed) < tolerance &&
        Math.abs(expected.critical_rate - character.critical_rate) < tolerance;

    return {
        isLegal,
        data: isLegal ? {} : expected // 只有非法时才返回需更新的数据
    };
}


// 导出函数
module.exports = {
    _validateCharacterData
}